﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;

namespace Shop.Common
{
    public class HandleJson
    {
        public static System.Net.Http.HttpResponseMessage ToJson(string message, bool bret)
        {
            Dictionary<string, object> dic = new Dictionary<string, object>();
            int result;
            if (bret) { result = 1; }
            else { result = -1; }
            dic.Add("result", result);
            dic.Add("resultdetail", message);            
            return ToJson(dic);
        }
        public static System.Net.Http.HttpResponseMessage ToJson(Dictionary<string, object> dic)
        {
            JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
            javaScriptSerializer.MaxJsonLength = Int32.MaxValue; //取得最大数值
            string s = javaScriptSerializer.Serialize(dic);
            System.Net.Http.HttpResponseMessage rval = new System.Net.Http.HttpResponseMessage { Content = new System.Net.Http.StringContent(s, System.Text.Encoding.GetEncoding("UTF-8"), "application/json") };
            return rval;
        }

        public static string GetJsonValueSingle(string json, string sKeyName)
        {
            Dictionary<string, object> dic = GetJsonValue(json);
            string strReturn = "";
            if (dic.ContainsKey(sKeyName))
            {
                object obj = dic[sKeyName];
                if (obj != null)
                {
                    strReturn = obj.ToString();
                }
            }
            return strReturn;
        }

        public static object ToJson_sub(DataTable dt)
        {
            JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
            javaScriptSerializer.MaxJsonLength = Int32.MaxValue; //取得最大数值
            //ArrayList arrayList = new ArrayList();
            List<object> list = new List<object>();
            //Dictionary<string, object> dictionary = new Dictionary<string, object>();
            foreach (DataRow dataRow in dt.Rows)
            {
                Dictionary<string, object> dictionary = new Dictionary<string, object>(); //实例化一个参数集合
                foreach (DataColumn dataColumn in dt.Columns)
                {
                    string value;
                    if (dataRow[dataColumn.ColumnName] == DBNull.Value)
                    { value = ""; }
                    else
                    { value = dataRow[dataColumn.ColumnName].ToString(); }
                    dictionary.Add(dataColumn.ColumnName, value);
                }
                list.Add(dictionary);
                //arrayList.Add(dictionary); //ArrayList集合中添加键值
            }
            return list;
            //return arrayList;
            //return javaScriptSerializer.Serialize(arrayList); //返回一个json字符串
        }

        public static Dictionary<string, object> GetJsonValue(string json)
        {
            string s = json.Replace("{", "").Replace("}", "").Replace("\r", "").Replace("\n", "").Replace(" ", "").Replace("\0", "").Replace("\t", "")
                .Replace("'", "").Replace("\"", "");
            string[] ss = s.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
            string[] sss;
            Dictionary<string, object> dfulldata = new Dictionary<string, object>();
            for (int i = 0; i < ss.Length; i++)
            {
                sss = ss[i].Split(new string[] { ":" }, StringSplitOptions.RemoveEmptyEntries);
                if (sss.Length == 2)
                { dfulldata.Add(sss[0], sss[1]); }
            }
            return dfulldata;
        }

        public static T getJsonValueObj<T>(string json)
        {
            var result = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<T>(json);
            return result;
        }
    }
}